package controller;

import java.util.List;
import persistence.entites.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author KakoSeven
 */
@ManagedBean
@SessionScoped
public class POIController extends Controller {

    private POI poi = null;
    private String typeString;
    private String tagString;
    private transient DataModel pois = null;
    private transient DataModel types = null;
    private transient DataModel tags = null;

    public POIController() {
        super();
        refreshLists();
    }

    public void refreshLists() {
        refreshPois();
        refreshTypes();
        refreshTags();
    }

    public void refreshPois() {
        pois = new ListDataModel();
        pois.setWrappedData(connMySQL.getPoiDAO().findAll());
    }

    public void refreshTypes() {
        types = new ListDataModel();
        types.setWrappedData(connMySQL.getTypeDAO().findAll());
    }

    public void refreshTags() {
        tags = new ListDataModel();
        List<Tag> lt = connMySQL.getTagDAO().findUnusedTag();
        tags.setWrappedData(lt);
    }

    public String index() {
        refreshLists();
        return "poilist";
    }

    public String ajouterDetail() {
        poi = new POI();
        typeString = " ";
        tagString = " ";
        return "poidetail";
    }

    public String modifierDetail() {
        POI tmpoi = (POI) pois.getRowData();
        poi = connMySQL.getPoiDAO().findById(tmpoi.getId());
        return "poidetail";
    }

    public String delete() {
        poi = (POI) pois.getRowData();
        connMySQL.getPoiDAO().delete(poi);

        return index();
    }

    public String update() {
        Type tp = connMySQL.getTypeDAO().findByName(typeString);
        poi.setType(tp);

        if (!tagString.equalsIgnoreCase("null")) {
            Tag oldTag = poi.getTag();

            Tag tmpTag = connMySQL.getTagDAO().findById(Integer.parseInt(tagString));

            poi.setTag(tmpTag);

            tmpTag.setPoi(poi);
            if(oldTag != null){
                connMySQL.getTagDAO().update(oldTag);
            }
            connMySQL.getTagDAO().update(tmpTag);
        }
        connMySQL.getPoiDAO().update(poi);

        return index();
    }

    public String add() {
        Type tp = connMySQL.getTypeDAO().findByName(typeString);
        poi.setType(tp);

        Tag tg = null;

        if (tagString.equalsIgnoreCase("aucun")) {
            poi.setTag(null);
        } else {
            tg = connMySQL.getTagDAO().findById(Integer.parseInt(tagString));
            poi.setTag(tg);
        }

        connMySQL.getPoiDAO().insert(poi);

        if (tg != null) {
            tg.setPoi(poi);
            connMySQL.getTagDAO().update(tg);
        }

        return index();

    }

    /**********************
     ***** Accesseurs *****
     **********************/

    public POI getPoi() {
        return poi;
    }

    public void setPoi(POI poi) {
        this.poi = poi;
    }


    public DataModel getPois() {
        return pois;
    }

    public void setPois(DataModel pois) {
        this.pois = pois;
    }

    public String getTagString() {
        return tagString;
    }

    public void setTagString(String tagString) {
        this.tagString = tagString;
    }

    public DataModel getTags() {
        return tags;
    }

    public void setTags(DataModel tags) {
        this.tags = tags;
    }

    public String getTypeString() {
        return typeString;
    }

    public void setTypeString(String typeString) {
        this.typeString = typeString;
    }

    public DataModel getTypes() {
        return types;
    }

    public void setTypes(DataModel types) {
        this.types = types;
    }
}
